/*
 *
 * 名称：九宫
 * 功能：奇门遁甲九宫运转逻辑
 * 作者：清河张
 * 时间：2025.05.11
 *
 */

#include "qmgong9.h"
#include "base/zhu.h"
#include <QJsonObject>

using namespace QMDJ;

QMGONG9::GONG::GONG(const XZGONG &g, QMGONG9* g9, int q3y6, const char* name):
    XZGONG(g), _q3y6(q3y6), ____(0), _g9(g9), _name(name), _mask(0)
{
}

void QMGONG9::GONG::qing_paipan()
{
    _mask = 0;//异常标志
    _wxxqs = -1;//宫的能级
    _shen = -1;
    _men = -1;
    _xing = -1;
    _dp = -1;
    _tp = -1;
    _yin = -1;
    _an = -1;
    _yongshenming.clear();//清除用神显示
    _beizhu.clear();//备注
    _xiangyi.clear();//象意
    _jiexi.clear();//解析
}

void QMGONG9::GONG::qing_neiguan()
{
    _mask = 0;
    _wxxqs = -1;
}

void QMGONG9::GONG::neiguan()
{
    _wxxqs = _g9->_bz._y._z.wx5() - wx5();
    _xing.wyu(_g9->_bz._y._z.wx5());

    if(!zhg_hu())
    {

        switch (_g9->_paras._jia) {
        case ZHU::SHII:
            maxing_he(_g9->_bz._s._z.maxing().id());//马星
            kongwang_he(_g9->_bz._s.kw());//空亡
            // kongwang_he(bz._r.kw());//空亡
            break;
        default:
            break;
        }

        mp_hu();//门迫
        rm_hu();//入墓
        jx_hu();//击刑
        yin_an_he();//隐干暗干能量状态

        _xing.gyu(wx5());
        _men.yu(wx5());
        _shen.yu(wx5());

        if(_g9->_jiju2 == this)
        {
            _g9->zhg()->_xing.wyu(_g9->_bz._y._z.wx5());
            _g9->zhg()->_xing.gyu(wx5());
        }
    }

    shengxiangyi();
    jiexi();
}

void QMGONG9::GONG::addmask(quint64 m)
{
    _mask |= m;
}

void QMGONG9::GONG::rmmask(quint64 m)
{
    _mask &= ~m;
}

bool QMGONG9::GONG::hasmask(quint64 m)
{
    return (m & _mask) != 0;
}

WUXING5::WXXQS QMGONG9::GONG::wxxqs() const
{
    return _wxxqs;
}

void QMGONG9::GONG::maxing_he(int z)
{
    if(izhi1() == z) addmask(MASK_MAXING1);
    if(izhi2() == z) addmask(MASK_MAXING2);
}

void QMGONG9::GONG::kongwang_he(ZHI12X2 kw)
{
    if(izhi1() == kw._z1 || izhi1() == kw._z2) addmask(MASK_KONGWANG1);
    if(izhi2() == kw._z1 || izhi2() == kw._z2) addmask(MASK_KONGWANG2);
}

void QMGONG9::GONG::mp_hu()
{
    if(_men.wx5() - wx5() == WUXING5::KEEEE)
        addmask(MASK_MENPO);
}

void QMGONG9::GONG::rm_hu()
{
    if(_dp.yu(izhi2()) == ZHSH12::MU ||
        _dp.yu(izhi1()) == ZHSH12::MU)
        addmask(MASK_RUMU1);

    if(_tp.yu(izhi2()) == ZHSH12::MU ||
        _tp.yu(izhi1()) == ZHSH12::MU)
        addmask(MASK_RUMU2);

    if(_g9->_jiju1 == this)
    {
        if(_g9->zhg()->_dp.yu(izhi2()) == ZHSH12::MU ||
            _g9->zhg()->_dp.yu(izhi1()) == ZHSH12::MU)
            addmask(MASK_RUMU1);
    }

    if(_g9->_jiju2 == this)
    {
        if(_g9->zhg()->_tp.yu(izhi2()) == ZHSH12::MU ||
            _g9->zhg()->_tp.yu(izhi1()) == ZHSH12::MU)
            addmask(MASK_RUMU2);
    }
}

void QMGONG9::GONG::yin_an_he()
{
    _an.yu(izhi1());
    _yin.yu(izhi1());
}

void QMGONG9::GONG::jx_hu()
{
    ZHI12 x;
    x = GANZHI::chujia(_dp)._z.xing();
    if(!x.invalid())
    {
        if(x == izhi1() || x == izhi2())
            addmask(MASK_JIXING1);
    }

    x = GANZHI::chujia(_tp)._z.xing();
    if(!x.invalid())
    {
        if(x == izhi1() || x == izhi2())
            addmask(MASK_JIXING2);
    }

    if(_g9->_jiju1 == this)
    {
        x = GANZHI::chujia(_g9->zhg()->_dp)._z.xing();
        if(!x.invalid())
        {
            if(x == izhi1() || x == izhi2())
                addmask(MASK_JIXING1);
        }
    }

    if(_g9->_jiju2 == this)
    {
        x = GANZHI::chujia(_g9->zhg()->_tp)._z.xing();
        if(!x.invalid())
        {
            if(x == izhi1() || x == izhi2())
                addmask(MASK_JIXING2);
        }
    }
}


void QMGONG9::GONG::shengxiangyi()
{
    if(zhg_hu())
    {
        _xiangyi = _xing.name();
        _xiangyi += "\n";
        _xiangyi += _tp.name();
        _xiangyi += "\n";
        _xiangyi += _dp.name();
        _xiangyi += "\n";
        return;
    }

    _xiangyi = _xing.name();

    _jumian = _men.name();
    _jumian += "门逢";
    _jumian += _shen.name();
    _jumian += "遇天";
    _jumian += _xing.name();
    _jumian += "星和";
    if(_g9->_jiju2 == this)
    {
        _xiangyi += _g9->zhg()->_xing.name();
        _jumian += "天";
        _jumian += _g9->zhg()->_xing.name();
        _jumian += "星（寄居）和";
    }

    _xiangyi += "  ";
    _xiangyi += _shen.name();
    _xiangyi += "  ";
    _xiangyi += _men.name();

    if(MASK_MENPO & _mask) _xiangyi += "【迫】";

    _xiangyi += "\n";
    _xiangyi += _tp.name();
    _jumian += _tp.name();
    if(_g9->_jiju2 == this )
    {
        _xiangyi += _g9->zhg()->_tp.name();
        _jumian += _g9->zhg()->_tp.name();
        _jumian += "（寄居）";
    }
    _jumian += "加";
    if( _mask & GANMASK2 )
    {
        _xiangyi += "【 ";
        if(_mask & MASK_RUMU2) _xiangyi += "墓 ";
        if(_mask & MASK_JIXING2)_xiangyi += "刑 ";
        _xiangyi += "】";
    }

    _xiangyi += "\n";
    _xiangyi += _dp.name();
    _jumian += _dp.name();

    if(_g9->_jiju1 == this)
    {
        _xiangyi += _g9->zhg()->_dp.name();
        _jumian += _g9->zhg()->_dp.name();
        _jumian += "（寄居）";
    }

    _jumian += "落";
    _jumian += name();
    _jumian += "宫；";

    if(_mask & GANMASK1)
    {
        _xiangyi += "【 ";
        if(_mask & MASK_RUMU1) _xiangyi += "墓 ";
        if(_mask & MASK_JIXING1)_xiangyi += "刑 ";
        _xiangyi += "】";
    }

    _xiangyi += "\n";

    if(_an.name()[0])
    {
        _xiangyi += "--";
        _xiangyi += _an.name();
    }

    if(_yin.name()[0])
    {
        _xiangyi += "~";
        _xiangyi += _yin.name();
    }

    if(_mask & ZHIMASK1)
    {
        _xiangyi += "\n";
        _xiangyi += "【 ";
        _xiangyi += zhi1().name();
        if(_mask & MASK_MAXING1) _xiangyi += "马 ";
        if(_mask & MASK_KONGWANG1)_xiangyi += "空 ";
        _xiangyi += "】";
    }

    if(_mask & ZHIMASK2)
    {
        _xiangyi += "【 ";
        _xiangyi += zhi2().name();
        if(_mask & MASK_MAXING2) _xiangyi += "马 ";
        if(_mask & MASK_KONGWANG2) _xiangyi += "空 ";
        _xiangyi += "】";
    }

    if(!_yongshenming.isEmpty())
    {
        _xiangyi += "\n\n";
        _xiangyi += "用神：";
        _xiangyi += _yongshenming;
    }

    if(!_beizhu.isEmpty())
    {
        _xiangyi += "\n";
        _xiangyi += "注：\"";
        _xiangyi += _beizhu;
        _xiangyi += "\"";
    }
}


void QMGONG9::GONG::jiexi()
{
    char numstr[64];
    int jd;
    GUA8 s, x;
    GUA64 g64;

    std::snprintf(numstr, sizeof(numstr), "数【%d %d %d %d】\n",
                  _id, _gua2, _ht1, _ht2);

    _jiexi = numstr;

    if(zhg_hu())
    {
        _jiexi += "宫【";
        _jiexi += name();
        _jiexi += "，";
        _jiexi += wx5().name();
        _jiexi += "，";
        _jiexi += _wxxqs.name();
        _jiexi += "】";
        return;
    }

    x = gua2();

    _jiexi += "星【天";
    _jiexi += _xing.name();
    _jiexi += "，";
    _jiexi += _xing.wx5().name();
    _jiexi += "，令";
    _jiexi += _xing.wxxqfw().name();
    _jiexi += "，宫";
    _jiexi += _xing.wxxqfg().name();

    s = gong9_xu_gong(_xing.id())->gua2();

    g64 = GUA64::fu(s,x);

    _jiexi += "，";
    _jiexi += s.wname();
    _jiexi += x.wname();
    _jiexi += g64.name();

    jd = _xing.id() - _xu;

    if(jd == 0) _jiexi += "，伏吟";
    else if(jd == 4 || jd == -4) _jiexi += "，反吟";

    if(_g9->_jiju2 == this)
    {
        const XING9& xing = _g9->zhg()->_xing;
        _jiexi +="】【天";
        _jiexi += xing.name();
        _jiexi += "，";
        _jiexi += xing.wx5().name();
        _jiexi += "，令";
        _jiexi += xing.wxxqfw().name();
        _jiexi += "，宫";
        _jiexi += xing.wxxqfg().name();
    }

    _jiexi += "】\n宫【";
    _jiexi += name();
    _jiexi += "，";
    _jiexi += wx5().name();
    _jiexi += "，";
    _jiexi += _wxxqs.name();

    if(_g9->_yy == (_xu < 4)) _jiexi += "，内盘";
    else _jiexi += "，外盘";

    _jiexi += "】\n神【";
    _jiexi += _shen.name();
    _jiexi += "，";
    _jiexi += _shen.wx5().name();
    _jiexi += "，";
    _jiexi += _shen.wxxqs().name();

    _jiexi += "】\n门【";
    _jiexi += _men.name();
    _jiexi += "门，";
    _jiexi += _men.wx5().name();
    _jiexi += "，";
    _jiexi += _men.wxxqs().name();

    s = gong9_xu_gong(_men.id())->gua2();
    g64 = GUA64::fu(s,x);

    _jiexi += "，";
    _jiexi += s.wname();
    _jiexi += x.wname();
    _jiexi += g64.name();


    jd = _men.id() - _xu;
    if(jd == 0) _jiexi += "，伏吟";
    else if(jd == 4 || jd == -4) _jiexi += "，反吟";

    _jiexi += "】\n星+宫【天";
    _jiexi += _xing.name();
    _jiexi += " + ";
    _jiexi += name();
    _jiexi += "】： ";
    _jiexi += _g9->_xinggong[_xu][_xing.id()] ;
    _jiexi += "；\n";

    if(_g9->_jiju2 == this)
    {
        const XING9& xing = _g9->zhg()->_xing;
        _jiexi += "星+宫【天";
        _jiexi += xing.name();
        _jiexi += " + ";
        _jiexi += name();
        _jiexi += "】： ";
        _jiexi += _g9->_xinggong[_xu][xing.id()] ;
        _jiexi += "；\n";
    }

    _jiexi += "天+地【";
    _jiexi += _tp.nengliang().name();
    _jiexi += "|";
    _jiexi += _tp.name();
    _jiexi += " + ";
    _jiexi += _dp.nengliang().name();
    _jiexi += "|";
    _jiexi += _dp.name();
    _jiexi += "】： ";
    _jiexi += _g9->_geju[_dp.id()][_tp.id()];

    if(_g9->_jiju1 == this)
    {
        const GAN10& dp = _g9->zhg()->_dp;
        _jiexi += "；\n天+地【";
        _jiexi += _tp.nengliang().name();
        _jiexi += "|";
        _jiexi += _tp.name();
        _jiexi += " + ";
        _jiexi += dp.nengliang().name();
        _jiexi += "|";
        _jiexi += dp.name();
        _jiexi += "】： ";
        _jiexi += _g9->_geju[dp.id()][_tp.id()];
    }

    if(_g9->_jiju2 == this)
    {
        const GAN10& tp = _g9->zhg()->_tp;
        _jiexi += "；\n天+地【";
        _jiexi += tp.nengliang().name();
        _jiexi += "|";
        _jiexi += tp.name();
        _jiexi += " + ";
        _jiexi += _dp.nengliang().name();
        _jiexi += "|";
        _jiexi += _dp.name();
        _jiexi += "】： ";
        _jiexi += _g9->_geju[_dp.id()][tp.id()];
    }

    _jiexi += "；\n暗+地【";
    _jiexi += _an.nengliang().name();
    _jiexi += "|";
    _jiexi += _an.name();
    _jiexi += " + ";
    _jiexi += _dp.nengliang().name();
    _jiexi += "|";
    _jiexi += _dp.name();
    _jiexi += "】： ";
    _jiexi += _g9->_geju[_dp.id()][_an.id()];

    _jiexi += "；\n隐+地【";
    _jiexi += _yin.nengliang().name();
    _jiexi += "|";
    _jiexi += _yin.name();
    _jiexi += " + ";
    _jiexi += _dp.nengliang().name();
    _jiexi += "|";
    _jiexi += _dp.name();
    _jiexi += "】： ";
    _jiexi += _g9->_geju[_dp.id()][_yin.id()];
    _jiexi += "；\n";
}

QMGONG9::QMGONG9(PARAS& paras): _gongs
    {
        //  三奇六仪          备用 --
        { *gong9_gid_gong(0), this, GAN10::WUUU, "离九"},
        { *gong9_gid_gong(1), this, GAN10::JIII, "坎一"},
        { *gong9_gid_gong(2), this, GAN10::GENG, "坤二"},
        { *gong9_gid_gong(3), this, GAN10::XINN, "震三"},
        { *gong9_gid_gong(4), this, GAN10::RENN, "巽四"},
        { *gong9_gid_gong(5), this, GAN10::GUII, "中五"},
        { *gong9_gid_gong(6), this, GAN10::DING, "乾六"},
        { *gong9_gid_gong(7), this, GAN10::BING, "兑七"},
        { *gong9_gid_gong(8), this, GAN10::YIII, "艮八"}
      }, _paras(paras), _jiju1(nullptr), _jiju2(nullptr),
    _mx(nullptr), _zf(nullptr), _zs(nullptr), _yy(false), _jd(0)
{
    reset();
}


QMGONG9::GONG *QMGONG9::teshu(int ts)
{
    return teshu(ts, _bz);
}

QMGONG9::GONG *QMGONG9::teshu(int ts, const BAZI & bz)
{
    GONG * g = nullptr;
    switch (ts) {
    case EQM_YS_ZHFU:
        g = _zf;
        break;
    case EQM_YS_ZHSH:
        g = _zs;
        break;
    case EQM_YS_NGAN:
        g = chazhao( &GONG::_tp, bz._n.wujia() )->ji1();
        break;
    case EQM_YS_YGAN:
        g = chazhao( &GONG::_tp, bz._y.wujia() )->ji1();
        break;
    case EQM_YS_RGAN:
        g = chazhao( &GONG::_tp, bz._r.wujia() )->ji1();
        break;
    case EQM_YS_SGAN:
        g = chazhao( &GONG::_tp, bz._s.wujia() )->ji1();
        break;
    case EQM_YS_NZHI:
        g = gzhi(bz._n._z.id());
        break;
    case EQM_YS_YZHI:
        g = gzhi(bz._y._z.id());
        break;
    case EQM_YS_RZHI:
        g = gzhi(bz._r._z.id());
        break;
    case EQM_YS_SZHI:
        g = gzhi(bz._s._z.id());
        break;
    default:
        g = zhg();
        break;
    }
    return g;
}

QMGONG9::GONG *QMGONG9::zhuan(int jd)//只转动寄居宫
{
    int order;
    _jd = XUNHUAN ( 0 + jd, 8 );
    if(_jiju1 && _jiju2)
    {
        if(_jiju1->_xu < 8)
        {
            order = XUNHUAN ( _jiju1->_xu + jd, 8 );
            _jiju2 = &_gongs[gong9_xu_gid(order)];
        }
    }
    return _jiju2;
}

const char *QMGONG9::GONG::gongname() const
{
    return SHUZI(_id).name();
}

const char *QMGONG9::GONG::name() const
{
    return _name;
}

QMGONG9::GONG *QMGONG9::GONG::ji0()
{
    return zhg_hu() && _g9->_jiju1 ? _g9->_jiju1 : this;
}

QMGONG9::GONG *QMGONG9::GONG::ji1()
{
    return zhg_hu() && _g9->_jiju2 ? _g9->_jiju2 : this;
}

QMGONG9::GONG *QMGONG9::GONG::fei(int jb)
{
    return &_g9->_gongs[XZGONG::fei(jb)];
}

QMGONG9::GONG *QMGONG9::GONG::xiantian()
{
    return &_g9->_gongs[XZGONG::xiantian()];
}

QMGONG9::GONG *QMGONG9::GONG::houtian()
{
    return &_g9->_gongs[XZGONG::houtian()];
}

QMGONG9::GONG *QMGONG9::mx()
{
    return _mx;
}

QMGONG9::GONG *QMGONG9::gzhi(int z)
{
    return &_gongs[::gong9_zhi_gid(z)];
}


QMGONG9::GONG* QMGONG9::zhg()
{
    return &_gongs[5];
}

QMGONG9::GONG *QMGONG9::xu(int x)
{
    return &_gongs[::gong9_xu_gid(x)];
}

QMGONG9::GONG *QMGONG9::gong(int g)
{
    return &_gongs[g%9];
}

QMGONG9::GONG *QMGONG9::gua1(int g)
{
    return &_gongs[::gong9_gua1_gid(g)];
}

QMGONG9::GONG *QMGONG9::gua2(int g)
{
    return &_gongs[::gong9_gua2_gid(g)];
}

void QMGONG9::neiguan()
{
    GONG* g;
    for(int i = 0; i < 9; ++i)
    {
        g = &_gongs[i];
        g->qing_neiguan();
        g->neiguan();
    }
}

void QMGONG9::ji4jiju(int j4)//哪个季度，坤二宫为3季
{
    int ju;
    if(j4)
    {
        ju = ((j4 % 4) * 2 - 1 + 8 ) % 8;
        _jiju1 = &_gongs[gong9_xu_gid(ju)] ;
        _jiju2 = &_gongs[gong9_xu_gid(ju)];
    }
    else
    {
        _jiju1 = nullptr;
        _jiju2 = nullptr;
    }
    _jd = 0;
}

void QMGONG9::gongjiju(int g)//哪个宫，坤二宫为2
{
    if(g)
    {
        _jiju1 = &_gongs[g % 9];
        _jiju2 = &_gongs[g % 9];
    }
    else
    {
        _jiju1 = nullptr;
        _jiju2 = nullptr;
    }
    _jd = 0;
}

int QMGONG9::jd() const
{
    return _jd;
}

void QMGONG9::feibu(GAN10 GONG::*m, int gong_start, int dw)//转布
{
    int g;

    for(int i = 0; i < 9; ++i)
    {
        g = XUNHUAN ( gong_start + i*dw, 9 );
        _gongs[g].*m = _gongs[i]._q3y6;
    }
}



void QMGONG9::prepare(const LIFA &ln)
{
    _lf = ln;

    _bz = _lf.bazi();
    reset();
}

QMGONG9::GONG * QMGONG9::yongshen(unsigned buluo, int bianhao, const BAZI& ms,
                                 const QByteArray& ming, QByteArray& bianzhi, QByteArray& nengji)
{
    GONG * ysgong = nullptr;
    GAN10 g;
    ZHI12 z;
    XING9 x;

    switch(buluo)
    {
    case EQM_YS_TESHU:
        switch (bianhao) {
        case EQM_YS_ZHFU:
            ysgong = _zf;
            bianzhi = QMDJMING(EQM_YS_ZHFU);
            nengji = ysgong->_shen.wxxqs().name();
            break;
        case EQM_YS_ZHSH:
            ysgong = _zs;
            bianzhi = QMDJMING(EQM_YS_ZHSH);
            nengji = ysgong->_men.wxxqs().name();
            break;
        case EQM_YS_NGAN:
            ysgong = teshu(EQM_YS_NGAN);
            bianzhi = QMDJMING(EQM_YS_NGAN);
            nengji = ysgong->_tp.nengliang().name();
            break;
        case EQM_YS_YGAN:
            ysgong = teshu(EQM_YS_YGAN);
            bianzhi = QMDJMING(EQM_YS_YGAN);
            nengji = ysgong->_tp.nengliang().name();
            break;
        case EQM_YS_RGAN:
            ysgong = teshu(EQM_YS_RGAN);
            bianzhi = QMDJMING(EQM_YS_RGAN);
            nengji = ysgong->_tp.nengliang().name();
            break;
        case EQM_YS_SGAN:
            ysgong = teshu(EQM_YS_SGAN);
            bianzhi = QMDJMING(EQM_YS_SGAN);
            nengji = ysgong->_tp.nengliang().name();
            break;
        case EQM_YS_NZHI:
            ysgong = teshu(EQM_YS_NZHI);
            bianzhi = QMDJMING(EQM_YS_NZHI);
            nengji = "--";
            break;
        case EQM_YS_YZHI:
            ysgong = teshu(EQM_YS_YZHI);
            bianzhi = QMDJMING(EQM_YS_YZHI);
            nengji = "--";
            break;
        case EQM_YS_RZHI:
            ysgong = teshu(EQM_YS_RZHI);
            bianzhi = QMDJMING(EQM_YS_RZHI);
            nengji = "--";
            break;
        case EQM_YS_SZHI:
            ysgong = teshu(EQM_YS_SZHI);
            bianzhi = QMDJMING(EQM_YS_SZHI);
            nengji = "--";
            break;
        default:
            ysgong = zhg();
            bianzhi = QMDJMING(EQM_WEIZHI);
            nengji = "--";
            break;
        }
        break;
    case EQM_YS_MINSH:
        bianzhi = "命";
        switch (bianhao) {
        case EQM_YS_NGAN:
            ysgong = teshu(EQM_YS_NGAN, ms);
            bianzhi += QMDJMING(EQM_YS_NGAN);
            nengji = ysgong->_tp.nengliang().name();
            break;
        case EQM_YS_YGAN:
            ysgong = teshu(EQM_YS_YGAN, ms);
            bianzhi += QMDJMING(EQM_YS_YGAN);
            nengji = ysgong->_tp.nengliang().name();
            break;
        case EQM_YS_RGAN:
            ysgong = teshu(EQM_YS_RGAN, ms);
            bianzhi += QMDJMING(EQM_YS_RGAN);
            nengji = ysgong->_tp.nengliang().name();
            break;
        case EQM_YS_SGAN:
            ysgong = teshu(EQM_YS_SGAN, ms);
            bianzhi += QMDJMING(EQM_YS_SGAN);
            nengji = ysgong->_tp.nengliang().name();
            break;
        case EQM_YS_NZHI:
            ysgong = teshu(EQM_YS_NZHI, ms);
            bianzhi += QMDJMING(EQM_YS_NZHI);
            nengji = "--";
            break;
        case EQM_YS_YZHI:
            ysgong = teshu(EQM_YS_YZHI, ms);
            bianzhi += QMDJMING(EQM_YS_NZHI);
            nengji = "--";
            break;
        case EQM_YS_RZHI:
            ysgong = teshu(EQM_YS_RZHI, ms);
            bianzhi += QMDJMING(EQM_YS_NZHI);
            nengji = "--";
            break;
        case EQM_YS_SZHI:
            ysgong = teshu(EQM_YS_SZHI, ms);
            bianzhi += QMDJMING(EQM_YS_NZHI);
            nengji = "--";
            break;
        default:
            ysgong = zhg();
            bianzhi += QMDJMING(EQM_WEIZHI);
            nengji = "--";
            break;
        }
        break;
    case EQM_YS_XING9:
        x = bianhao;
        ysgong = chazhao( &GONG::_xing, x)->ji1();
        bianzhi += "天";
        bianzhi += x.name();
        bianzhi += "星";
        x.gyu(ysgong->wx5());
        nengji = x.wxxqfg().name();
        break;
    case EQM_YS_MMEN8:
        ysgong = chazhao( &GONG::_men, bianhao)->ji1();
        bianzhi = ysgong->_men.name();
        bianzhi += "门";
        nengji = ysgong->_men.wxxqs().name();
        break;
    case EQM_YS_SHEN8:
        ysgong = chazhao( &GONG::_shen, bianhao)->ji1();
        bianzhi = ysgong->_shen.name();
        nengji = ysgong->_shen.wxxqs().name();
        break;
    case EQM_YS_TPGAN:
        g = bianhao;
        ysgong = chazhao( &GONG::_tp, g)->ji1();
        bianzhi = g.name();
        bianzhi += g.wx5().name();
        g.yu(ysgong->izhi1());
        nengji = g.nengliang().name();
        break;
    case EQM_YS_DPGAN:
        g = bianhao;
        ysgong = chazhao( &GONG::_dp, g)->ji1();
        bianzhi = g.name();
        bianzhi += g.wx5().name();
        g.yu(ysgong->izhi1());
        nengji = g.nengliang().name();
        break;
    case EQM_YS_GONG9:
        ysgong = &_gongs[bianhao];
        bianzhi = ysgong->name();
        bianzhi += "宫";
        nengji = ysgong->wxxqs().name();
        break;
    case EQM_YS_XTGUA:
        ysgong = gua1(bianhao);
        bianzhi = ysgong->gua1().name();
        nengji = "--";
        break;
    case EQM_YS_HTGUA:
        ysgong = gua2(bianhao);
        bianzhi = ysgong->gua2().name();
        nengji = "--";
        break;
    case EQM_YS_DIZHI:
        z = bianhao;
        ysgong = gzhi(z.id());
        bianzhi = z.name();
        bianzhi += z.wx5().name();
        nengji = "--";
        break;
    default:
        ysgong = zhg();
        bianzhi = "未知部落";
        nengji = "--";
        break;
    }

    if(ysgong)
    {
        ysgong->_yongshenming += " ";
        ysgong->_yongshenming += ming;
    }

    return ysgong;
}

void QMGONG9::shezhifangan(const QJsonObject &&jsonGeJu,
                           const QJsonObject &&jsonXingGong)
{
    for(int i = 0; i < 9; ++i)
    {
        QJsonObject&& jsonJG = jsonXingGong[QString::number(i)].toObject();
        QJsonObject&& jsonDP = jsonGeJu[QString::number(i+1)].toObject();
        for(int j = 0; j < 9; ++j)
        {
            _xinggong[i][j] = jsonJG[QString::number(j)].toString().toUtf8();
            _geju[i+1][j+1] = jsonDP[QString::number(j+1)].toString().toUtf8();
        }
    }
}

int QMGONG9::shijia_paipan(int ju, int yy, const GANZHI& gz, GAN10& qi,
                           GAN10& luo)
{
    int dw;
    int jd;
    GONG* zfzs;//值符值使本宫

    _mx = gzhi(gz._z.maxing().id());

    _yy = yy;
    dw = yy ? 1 : -1;

    zfzs = _gongs[ju%9].fei( gz.xun() * dw )->ji0();//值班宫

    qi = gz.dun();
    luo = gz.wujia();

    //地盘军排局
    feibu(&GONG::_dp, ju, dw );

    //找干支对应宫位
    _zf = chazhao(&GONG::_dp, luo)->ji0();

    //旋转角度,多少格
    jd = zfzs->jd(_zf);
    zhuan(jd);//转寄居

    //星
    zhuanbu(&GONG::_xing);
    zhuandong(&GONG::_xing, jd);

    //天盘军
    feibu(&GONG::_tp, ju, dw);
    zhuandong(&GONG::_tp , jd);

    //排神
    zhuanbu(&GONG::_shen, 8, _zf->_xu, dw);

    //排门
    zhuanbu(&GONG::_men, 8);
    _zs = zfzs->fei(gz.wei() * dw)->ji0();
    jd = zfzs->jd(_zs);
    zhuandong(&GONG::_men, jd);

    //暗干fei
    if(_zs->_dp == gz.wujia()){
        feibu(&GONG::_an, 5 - chazhao3q6y(gz.wujia())*dw, dw );
    }else{
        feibu(&GONG::_an, _zs->_id - chazhao3q6y(gz.wujia())*dw, dw );
    }

    //隐干
    feibu(&GONG::_yin, ju, dw );
    jd = _zf->jd(_zs);
    zhuandong(&GONG::_yin , jd);

    //标准检查
    neiguan();

    return 0;
}

int QMGONG9::yuejia_paipan(int, int, const GANZHI&, GAN10&, GAN10&)
{
    return 0;
}

int QMGONG9::rijia_paipan(int, int, const GANZHI &, GAN10 &, GAN10 &)
{
    return 0;
}

void QMGONG9::reset()
{
    for(int i = 0; i < 9; ++i)
    {
        _gongs[i].qing_paipan();
    }
    _yy = false;
    _zf = nullptr;
    _zs = nullptr;
    _mx = nullptr;
}

const QByteArray& QMGONG9::chuduan()
{
    WUXING5::SHENGKE sk;

    GONG* rg = chazhao( &GONG::_tp, _bz._r.wujia())->ji1();
    GONG* sg = chazhao( &GONG::_tp, _bz._s.wujia())->ji1();

    _jxstr = "\n月令【";
    _jxstr += _bz._y._z.name();
    _jxstr += "月，";
    _jxstr += _bz._y._z.wx5().name();

    _jxstr += "】\n日干（命主）落【";
    _jxstr += rg->name();
    _jxstr += "】\n被时【";
    _jxstr += sg->name();
    _jxstr += "】所";

    sk = sg->wx5() - rg->wx5();
    _jxstr += sk.name();

    _jxstr += "\n被符【";
    _jxstr += _zf->name();
    _jxstr += "】所";
    sk = _zf->wx5() - rg->wx5();
    _jxstr += sk.name();


    _jxstr += "\n被使【";
    _jxstr += _zs->name();
    _jxstr += "】所";
    sk = _zs->wx5() - rg->wx5();
    _jxstr += sk.name();
    _jxstr += "\n";

    return _jxstr;
}

QString QMGONG9::jumian() const
{
    QString jumian = "奇门起局使用八字“";//局面
    jumian += _bz.name();
    jumian += "”；盘面：";

    for (int i = 0; i < 9; ++i)
    {
        jumian += _gongs[i]._jumian;
    }
    jumian += _zs->_men.name();
    jumian += "门为值使。";

    return jumian;
}

void QMGONG9::nianjia(int &ju, int &yy, GAN10 &qi, GAN10 &luo)
{
    yy = 0;
    ju = nianjiadingju();
    shijia_paipan(ju, yy, _bz._n, qi, luo);
}

void QMGONG9::yuejia(int &ju, int &yy, GAN10 &qi, GAN10 &luo)
{
    yy = 0;
    ju = yuejiadingju();
    yuejia_paipan(ju, yy, _bz._y, qi, luo);
}

void QMGONG9::rijia(int &ju, int &yy, GAN10 &qi, GAN10 &luo)
{
    yy = _lf.jieqi().yinyang();
    ju = rijiadingju();
    rijia_paipan(ju, yy, _bz._r, qi, luo);
}

void QMGONG9::shijia(int &ju, int &yy, GAN10 &qi, GAN10 &luo)
{
    int yuan = 0;
    JIE24 jq;
    if(YINPAN == _paras._dingju)
    {
        ju = yinpan(jq);
        yy = jq.yinyang();
    }
    else
    {
        switch(_paras._dingju)
        {
        case MAOSHAN:
            yuan = maoshanyuan(jq);
            break;
        case CHAIBU:
            yuan = chaibuyuan(jq);
            break;
        case ZHIRUN:
            yuan = zhirunyuan(jq);
            break;
        case ZHANGSHI:
            yuan = zhangshiyuan(jq);
            break;
        default:
            break;
        }
        yy = jq.yinyang();
        ju = shijiadingju( jq.jie(), yuan, yy);
    }

    shijia_paipan(ju, yy, _bz._s, qi, luo);
}


int QMGONG9::maoshanyuan(JIE24 & jq)
{
    int yuan;
    jq = _lf.jieqi();

    yuan = jq.yufen(DAYTOSENCONDS) / 5;
    if(yuan > 2) yuan = 2;
    return yuan;
}

int QMGONG9::chaibuyuan(JIE24& jq)
{
    jq = _lf.jieqi();
    int r = _bz._r.gz60();
    return r % 15 / 5;
}

int QMGONG9::zhirunyuan(JIE24& jq)
{
    int gz15, gz15x;
    bool chaoshen = true;//超神
    JIE24 jq0,jq1,jq2;//原始节气，前后节气
    LIFA lf0, ft1, ft2;//原始时间，前后符头

    gz15 = _bz._r.gz60() % 15;

    ft1 = _lf.addDays(-gz15);
    ft2 = _lf.addDays(15-gz15);

    jq = _lf.jieqi();//当天节气

    jq1 = ft1.jieqi();
    jq2 = ft2.jieqi();

    if(jq1 == JIEQI_T::MANGZHONG || jq1 == JIEQI_T::DAXUE)
    {
        if(jq1.yufen(DAYTOSENCONDS) < 6) chaoshen = false;//置闰成接气
    }
    else
    {
        jq0 = jq.xiangqian(jq.yinyang() ? JIEQI_T::DAXUE : JIEQI_T::MANGZHONG, 0);
        lf0 = LIFA::fromgl(LIFA::jq2datetime(jq0));
        gz15x = lf0.bazi()._r.gz60() % 15;
        if(gz15x > 8) chaoshen = false;//上次刚置闰
    }

    jq = chaoshen ? jq2 : jq1;
    return gz15 / 5;
}

/*
 *
 * 张氏定局法。 这个方法是张岩客师门独有的。
 * 交接前后2日见甲己，提前或者延后换局。
 */
int QMGONG9::zhangshiyuan(JIE24& jq)
{
    int gz15,gz5;
    LIFA lf;

    gz15 = _bz._r.gz60() % 15;
    gz5 = gz15 % 5;

    if(gz5 > 2)//前面的节气
        lf = _lf.addDays(-2);//前2天时间
    else if(gz5 < 2)//后面的节气
        lf = _lf.addDays(2);//后2天时间
    else
        lf = _lf;

    jq = lf.jieqi();
    return gz15 / 5;
}

int QMGONG9::yinpan(JIE24& jq)
{
    int ns,ss,ju;
    jq = _lf.jieqi();
    QDate nl0 = _lf.nl();
    ns = (_bz._n._z.id() - ZHI12::HAII + 12) % 12;
    if(!ns) ns = 12;

    ss = (_bz._s._z.id() + 1);
    ju = (ns + nl0.month() + nl0.day() + ss ) % 9;
    if(!ju) ju = 9;
    return ju;
}

int QMGONG9::nianjiadingju() const
{
    int yuan;
    yuan  = _lf.dayun() / 3;
    return yuan * 3 + 1;
}

int QMGONG9::yuejiadingju() const
{
    int nz,yuan;
    nz = _bz._n._z.id();
    yuan = ( nz + 1 ) % 3;
    return yuan * 3 + 1;
}

int QMGONG9::rijiadingju() const
{
    return 0;
}

int QMGONG9::shijiadingju(int hou24, int yuan, int yy) const
{
    int oid, xu, id, dw, gid;

    oid = ( hou24 / 3 ) % 8;
    gid = gong9_xu_gid(oid);

    xu = hou24 % 3;
    dw = yy ? 1 : -1;
    id = ( gid - yuan * ( 3 * dw ) + xu * dw + 18 ) % 9;
    return id + (!id)*9;
}

int QMGONG9::chazhao3q6y(GAN10 g) const
{
    //奇门格局十干克应
    for(unsigned i = 0; i < 9; ++i)
        if (g == _gongs[i]._q3y6) return i;
    return -1;
}
